package cn.cycleif.thread;

import cn.hutool.core.thread.ThreadFactoryBuilder;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.concurrent.*;

/**
 * @Date: 25/1/19-22:25
 * @Desc:
 */
public class BizTreadPool {

  private static final ThreadPoolExecutor executor;

  static {
    executor = new ThreadPoolExecutor(

        Runtime.getRuntime()
               .availableProcessors() + 1,
        maximumPoolSize(),
        60,
        TimeUnit.SECONDS,
        new ArrayBlockingQueue<>(600),
        new ThreadFactoryBuilder().setNamePrefix("task-pool--")
                                  .build(),
        new ThreadPoolExecutor.CallerRunsPolicy());
  }

  private static Integer maximumPoolSize() {
    return new BigDecimal(Runtime.getRuntime()
                                 .availableProcessors())
        .divide(new BigDecimal("0.2"), 0, RoundingMode.HALF_UP)
        .intValue();
  }

  public static void execute(Runnable r) {
    executor.execute(r);
  }


  public static <T> Future<T> submit(Callable<T> c) {
    return executor.submit(c);
  }
}
